﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;

public partial class Posts_PostControlPopup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            int postId = 0;
            int templateControlId = 0;
            AjaxId = Request.QueryString["aid"];
            int siteId = 0;
            DateTime? siteVersion = null;
            try
            {
                postId = int.Parse(Request.QueryString["pid"]);
                templateControlId = int.Parse(Request.QueryString["tcid"]);
                siteId = int.Parse(Request.QueryString["sid"]);
                if (Request.QueryString["svid"] != null)
                {
                    siteVersion = DateTime.Parse(Request.QueryString["svid"]);
                }
            }
            catch
            {
            }
            if (postId > 0 && templateControlId > 0)
            {
                PostControl postControl = PostControl.GetByPostIdAndTemplateControlId(postId, templateControlId);
                if (postControl != null)
                {
                    Title = string.Concat("Edit ", postControl.Name);
                    contentControl.DataSource = new PostControl[] { postControl };
                    contentControl.DataBind();
                    loader.PostControl = postControl;
                    loader.SiteVersion = siteVersion;
                    loader.DataBind();
                }
            }
        }
    }

    protected string AjaxId { get; set; }

    protected override object SaveViewState()
    {
        ViewState["AjaxId"] = AjaxId;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        AjaxId = ViewState["AjaxId"] as string;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            bool saved = false;
            /*Acms.AcmsControls.ContentControls.Admin.BaseAdminEdit adminControl = loader.Control as Acms.AcmsControls.ContentControls.Admin.BaseAdminEdit;
            if (adminControl != null)
            {
                saved = true;
                adminControl.Save();
            }
            string javascript = string.Format("returnToParent({0}, \"{1}\");", saved.ToString().ToLower(), AjaxId);
            this.ClientScript.RegisterStartupScript(this.GetType(), "SaveCompleted", javascript, true);
             */
        }
    }

}
